using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using Net.SamuelChen.Production.SmartAssistant.Calendar;

namespace Net.SamuelChen.Production.SmartAssistant {
    static class Program {

        private static System.Windows.Forms.NotifyIcon notifyIcon;
        private static System.Windows.Forms.ContextMenu notifyMenu;
        private static fmCalendar Calendar = null;

        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));


        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            log4net.Config.BasicConfigurator.Configure();
            log.Info("===== " + Name + " Application Started =====");

            InitThemes();

            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Properties.Settings.Default.Local);

            InitUI();

            // singleton check
            Mutex appSingleton = new Mutex(false, Name + " Singleton Tag");

            if (appSingleton.WaitOne(0, false))
                Application.Run();

            appSingleton.Close();
        }

        #region Public Application Properties

        public static string Name {
            get {
                return "Google Helper";
            }
        }

        public static string Author {
            get {
                return "Samuel Chen";
            }
        }

        public static string HomePage {
            get {
                return "http://www.SamuelChen.Net";
            }
        } 
        #endregion

        #region Initiallization
        // User interface & events initiallization
        private static void InitUI() {
            //
            // notifyMenu
            //
            notifyMenu = new ContextMenu();
            MenuItem mainMenuItem;

            mainMenuItem = new MenuItem("&Activate");
            mainMenuItem.DefaultItem = true;
            notifyMenu.MenuItems.Add(mainMenuItem);
            mainMenuItem.Click += new EventHandler(MainMenuActivate_Click);

            mainMenuItem = new MenuItem("&Exit");
            notifyMenu.MenuItems.Add(mainMenuItem);
            mainMenuItem.Click += new EventHandler(MainMenuExit_Click);

            // 
            // NotifyIcon
            // 
            notifyIcon = new NotifyIcon();
            notifyIcon.Text = Name;
            notifyIcon.Visible = true;
            notifyIcon.Icon = Properties.Resources.icoSmartAssistant;
            notifyIcon.ContextMenu = notifyMenu;

            //
            // Events registration
            //
            Microsoft.Win32.SystemEvents.SessionEnded +=
                new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
            notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);
            notifyIcon.Click += new EventHandler(notifyIcon_Click);
        }

        // Window themes initiallization
        private static void InitThemes() {
            if (((Environment.OSVersion.Platform == PlatformID.Win32NT)
                && (Environment.OSVersion.Version.Major >= 5))
                && (Environment.OSVersion.Version.Minor > 0)) {

                if (OSFeature.Feature.IsPresent(OSFeature.Themes)) {
                    Application.EnableVisualStyles();
                }

                Application.DoEvents();
            }
        } 
        #endregion

        #region Events
        // Application exit
        private static void SystemEvents_SessionEnded(object sender, Microsoft.Win32.SessionEndedEventArgs e) {
            if (Calendar != null) {
                Calendar.Close();
                notifyIcon.Visible = false;
            }
            log.Info("===== " + Name + " Application Stoped ====");
            Application.Exit();
        }

        private static void notifyIcon_DoubleClick(object sender, EventArgs e) {
            if (Calendar != null)
                Calendar.Show();
            else {
                Calendar = new fmCalendar();
                Calendar.Show();
            }
            Calendar.Focus();
        }

        private static void notifyIcon_Click(object sender, EventArgs e) {

        }

        private static void MainMenuActivate_Click(object sender, EventArgs e) {
            notifyIcon_DoubleClick(sender, e);
        }

        private static void MainMenuExit_Click(object sender, EventArgs e) {
            if (Calendar != null) {
                Calendar.Close();
                notifyIcon.Visible = false;
            }
            log.Info("===== " + Name + " Application Stoped ====");
            Application.Exit();
        }
        
        #endregion
    }
}